//测试分页的demon
	var totalPage =2;//一共多少页数据
	var pageSize =6;//每页显示10条数据
	var currentPage =1;//当前第几页数据，默认为1
	var isLoading = false;//是否正在加载更多数据//
$(function(){
	//重置
	$('#btn-reset').click(function(){
		$('#search-categoryName').val("");
		/*query(1,"");*/
	});
	
	query(1);
   function query(pageNum){
	      var p ={};
	          p.pageSize = pageSize;
	          p.pageNum = pageNum;
		  $('#user-category').empty();
		  $('#user-category').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>种类名</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/categoryConntroller/findAllCategory",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result.list;
           for(var i=0;i<array.length;i++){
        	    var html = '' ;
        		html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].movieCategory+'</td></tr>';
           	    $('#user-category').append(html); 
           }
		   //总共多少页
           $('#paginator').empty();
           var totalPages = data.result.pages;
        	   var pageHtml = '<ul>';
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
        	   for(var i=0;i<totalPages;i++){
        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
        	   }
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
        	   pageHtml =pageHtml+'</ul>';
        	   $('#paginator').append(pageHtml);
		  }
	
	   });
   }
 
 //查询信息
	$('#btn-search').click(function(){
   var categoryName =	$('#search-categoryName')[0].value;
   querySearch(1,categoryName);
	
	});
	
	  //带有查询条件的分页
	   function querySearch(pageNum,parameter){
		  var p={};
		     p.pageSize = pageSize;
		     p.pageNum = pageNum;
		     p.parameter = parameter;
			  $('#user-category').empty();
			  $('#user-category').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>种类名</td></tr>');
		$.ajax({
			  type:"POST",
			  url:"/movie.front/categoryConntroller/findAllCategory",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
	           array = data.result.list;
	           for(var i=0;i<array.length;i++){
	        	    var html = '' ;
	        		html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].movieCategory+'</td></tr>';
	           	    $('#user-category').append(html); 
	           }
			   //总共多少页
	           $('#paginator').empty();
	           var totalPages = data.result.pages;
	        	   var pageHtml = '<ul>';
	        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
	        	   for(var i=0;i<totalPages;i++){
	        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
	        	   }
	        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
	        	   pageHtml =pageHtml+'</ul>';
	        	   $('#paginator').append(pageHtml);
			  }
		
		   }); 
		     
	   }
   //添加信息
	$('#btn-insert').click(function(){
		$('#categoryName').val("");
		$('#myModal').modal('show');
		/*alert('aaaaaaa');*/
	});
	
	$('#btn-save-category').click(function(){
		var movieCategory =	$('#categoryName')[0].value;
		var id = $('#id')[0].value;
		if(id==null || id==""){
			var p={movieCategory:movieCategory};
		$.ajax({
			  type:"POST",
			  url:"/movie.front/categoryConntroller/addCategory",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
	            if(data.successCode!=null){
	            	//关闭对话框
	            	$('#myModal').modal('hide');
	            	//刷新表格
	            	var categoryName=null;
	            	query(1,categoryName);
	            	
	            }
	            if(data.errorcode!=null){
	            	alert(data.errorcode);
	            }
			   
			  }
		
		   });
		}else{
			var p={id:id,movieCategory:movieCategory};
			$.ajax({
				  type:"POST",
				  url:"/movie.front/categoryConntroller/updateCategory",
				  data:JSON.stringify(p),
				  dataType:"json",
				  contentType:"application/json",
				  success:function(data){
					  if(data.successCode!=null){
						  $('#myModal').modal('hide');
							//刷新表格
						  var categoryName=null;
			            	query(1,categoryName);				  
					  }
					  if(data.errorcode!=null){
			            	alert(data.errorcode);
			            }

				   
				  }
			
			   });
		 }
		});
		
		//修改用户信息
		$('#btn-update').click(function(){
			var flag=0;
			var id =0;
			var array = $(("input[type='checkbox']"));
			for(var i=0;i<array.length;i++){
			  console.info(array[i].id);
			  if($('#'+array[i].id).is(':checked')){
				var id = array[i].id;
			      flag++;
			      if(flag>1){
			    	  alert("对不起，只能选择单行");
			    	return false;
			      }else{
			    	  id=array[i].id;
			      }
			
			  }
		   
			}
			  if(flag==1){
				  $('#myModal').modal('show');
				  var id = {id:id};
				  getByIdUserInfo(id);
			     }
		});
		
		//删除用户信息
		$('#btn-remove').click(function(){
			var flag=0;
			var id=0;
			var array = $(("input[type='checkbox']"));
			for(var i=0;i<array.length;i++){
			  console.info(array[i].id);
			  if($('#'+array[i].id).is(':checked')){
			      flag++;
			      if(flag>1){
			    	  alert("对不起，只能选择单行");
			    	 return false;
			      }else{
			    	  var id = array[i].id;
			      }
			
			  }
		   
			}
			  if(flag==1){
			    	 var id = {id:id};
			    	 deleteUserInfo(id);
			      }
			
		});
		function deleteUserInfo(p){
			$.ajax({
				  type:"POST",
				  url:"/movie.front/categoryConntroller/deleteCategory",
				  data:JSON.stringify(p),
				  dataType:"json",
				  contentType:"application/json",
				  success:function(data){
					  if(data.successCode!=null){
						  alert("删除成功");
						  $('#myModal').modal('hide');
							//刷新表格
						  var categoryName=null;
			            	query(1,categoryName);				  
					  }
					  if(data.errorcode!=null){
			            	alert(data.errorcode);
			            }
					 
					 
					  
				     }
			   });
		}
		function getByIdUserInfo(p){
			//回显信息
		
			$.ajax({
				  type:"POST",
				  url:"/movie.front/categoryConntroller/getCategoryById",
				  data:JSON.stringify(p),
				  dataType:"json",
				  contentType:"application/json",
				  success:function(data){
		           $('#categoryName')[0].value=data.result.movieCategory;
		           $('#id')[0].value=data.result.id;
				  }
			
			   });
		}
		
});
	
//当前页
function currentPagesquery(currentId){
	 // alert(currentId);当前页码
	  $('#paginator').empty();
	     var p ={};
         p.pageSize = 6;
         p.pageNum = currentId;
	  $('#user-category').empty();
	  $('#user-category').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>种类名</td></tr>');
    $.ajax({
	  type:"POST",
	  url:"/movie.front/categoryConntroller/findAllCategory",
	  data:JSON.stringify(p),
	  dataType:"json",
	  contentType:"application/json",
	  success:function(data){
      array = data.result.list;
      if(currentId==1){
      for(var i=0;i<array.length;i++){
   	    var html = '' ;
   		html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].movieCategory+'</td></tr>';
      	    $('#user-category').append(html); 
         }
      }
      else{
    	   for(var i=0;i<array.length;i++){
    		   var flag = (currentId-1)*6+i+1;
    	   	    var html = '' ;
    	   		html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+flag+'</td><td>'+array[i].movieCategory+'</td></tr>';
    	      	    $('#user-category').append(html); 
    	         }
      }
      
	   //总共多少页
      var totalPages = data.result.pages;
   	   var pageHtml = '<ul>';
   	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
   	   for(var i=0;i<totalPages;i++){
   		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
   	   }
   	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
   	   pageHtml =pageHtml+'</ul>';
   	   $('#paginator').append(pageHtml);
	  }

  });
}

//首页
function  firstPagesquery(firstPageId){
	currentPagesquery(firstPageId);
}

//尾页
function  endPagequery(endPageId){
	currentPagesquery(endPageId);
	 }
